package cn.uyunsky.platform.taglib;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;

public class ImportTag extends TagSupport {

	private static final long serialVersionUID = 1L;
	private String src;

	public int doStartTag() throws JspException {
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
        StringBuilder sb = new StringBuilder();
        String srcpath = request.getContextPath() + src;
		if (src.indexOf(".css") == -1) {
            sb.append("<script type=\"text/javascript\" src=\"");
            sb.append(srcpath);
            sb.append("\"></script>");
		} else {
            sb.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"");
            sb.append(srcpath);
            sb.append("\" />");
		}
		try {
			pageContext.getOut().write(sb.toString());
		} catch (IOException e) {
			e.printStackTrace();
			throw new JspException(e);
		}
		return SKIP_BODY;
	}

	/**
	 */
	public int doEndTag() throws JspException {
		return EVAL_PAGE;
	}

	/**
	 */
	public void release() {
		src = null;
		super.release();
	}

	public void setSrc(String src) {
		this.src = src;
	}
}
